<template>
	<view>
		<view class="u-demo-block__content" style="margin: 15rpx;">
			<view class="u-page__tag-item">
				<u-search placeholder="输入工单号、物料名称、规格模糊查询" 
				v-model="bomInfoParam" 
				@custom="queryHistoryByParam"
				@search="queryHistoryByParam"></u-search>
			</view>
		</view>
		
		<view class="content">
			<view style="margin: 4px;">
				<uni-section :title="`合同号：${contractCode}`" type="line"></uni-section>
			</view>
			<view style="margin: 4px;">
				<uni-section :title="`报工班组：${teamInfo ? teamInfo.deptName : teamId}`" type="line"></uni-section>
			</view>
			
			<view class="itme-box">
				<uni-table ref="table" :loading="detailLoading"
					border
					stripe 
					type="" 
					emptyText="暂无更多数据">
				<uni-tr style="background-color: #dbfbdb;">
					<uni-th align="left" width="5" style="font-size: 12px;color: #62c188;">序号</uni-th>
					<uni-th align="left" width="90" style="font-size: 12px;color: #62c188;">报工日期</uni-th>
					<uni-th align="left" width="50" style="font-size: 12px;color: #62c188;">报工时间</uni-th>
					<uni-th align="left" width="50" style="font-size: 12px;color: #62c188;">报工人</uni-th>
					<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">合同号</uni-th>
					<uni-th align="left" width="80" style="font-size: 12px;color: #62c188;">工单号</uni-th>
					<uni-th align="left" width="80" style="font-size: 12px;color: #62c188;">产品名称</uni-th>
					<uni-th align="left" width="70" style="font-size: 12px;color: #62c188;">产品规格</uni-th>
					<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">订单数量</uni-th>
					<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">当天累计合格数量</uni-th>
					<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">当天累计不良品数量</uni-th>
					<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">累计完工数量</uni-th>
					<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">还欠数量</uni-th>
					<uni-th align="left" width="50" style="font-size: 12px;color: #62c188;">产品编码</uni-th>
					<uni-th align="left" width="50" style="font-size: 12px;color: #62c188;">站点名称</uni-th>
					<uni-th align="left" width="100" style="font-size: 12px;color: #62c188;">报工备注</uni-th>
					<uni-th align="left" width="100" style="font-size: 12px;color: #62c188;">操作</uni-th>
				</uni-tr>
				<uni-tr v-for="(productItem, subIndex) in contractHistoryDetailList" :key="subIndex">
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ subIndex+1 }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ productItem.submitDate }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ productItem.submitTime }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ productItem.creator }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ productItem.contractCode }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ productItem.workerorderCode }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ productItem.bomName }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ productItem.bomStandard }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ productItem.workorderNum }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ productItem.goodNum }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ productItem.ungoodNum }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ productItem.hasCompletionNum }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ productItem.remainingCompletionNum }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ productItem.bomCode }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ productItem.stationName }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ productItem.remark }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">
						<button type="primary" size="mini" @click="updateShow(productItem)">修改</button>
					</uni-td>
				</uni-tr>
				</uni-table>
			</view>
		</view>
			
		<view v-if="contractHistoryDetailList.length === 0" style="padding-top: 60px;">
			<u-empty mode="data" v-if="!loadingShow"></u-empty>
		</view>
		
		<!-- 报工提交确认数据 -->
		<u-popup :show="submitConfirmShow" mode="top"  @close="submitConfirmClose" :closeOnClickOverlay="false">
			<view style="max-height: 590rpx; overflow-y: auto;">
				<uni-table ref="table" :loading="submitConfirmLoading"
					border
					stripe 
					type="" 
					emptyText="暂无更多数据">
				<uni-tr style="background-color: #dbfbdb;">
					<uni-th align="left" width="5" style="font-size: 12px;color: #62c188;">序号</uni-th>
					<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">物料编码</uni-th>
					<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">物料名称</uni-th>
					<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">规格</uni-th>
					<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">当天合格数</uni-th>
					<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">当天不良品数</uni-th>
					<uni-th align="left" style="font-size: 12px;color: #62c188;">备注</uni-th>
				</uni-tr>
				
				<uni-tr v-for="(item, index) in completionWorkList" :key="index">
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ index+1 }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ item.bomCode }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ item.bomName }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ item.bomStandard }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ item.todayCompletionReq }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ item.ungoodNumReq }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ item.remark }}</uni-td>
				</uni-tr>
				</uni-table>
			</view>
			<view>
				<uni-section title="生产异常" type="line" padding>
					<uni-easyinput type="text" v-model="errMessage" placeholder="请输入生产异常信息"></uni-easyinput>
				</uni-section>
				<u-row>
					<u-col span="6">
						<u-button
							type="error"
							text="关闭"
							shape="circle" 
							customStyle="width: 260rpx;"
							@click="submitConfirmShow = !submitConfirmShow"
						></u-button>
					</u-col>
					<u-col span="6">
						<u-button
							type="primary"
							text="提交"
							shape="circle" 
							customStyle="width: 260rpx;"
							@click="submitReally"
						></u-button>
					</u-col>
				</u-row>
			</view>
		</u-popup>
		
		<u-calendar 
			@confirm="calendarConfirm"
			@close="calendarClose"
			title="已报工日期选择查询"
			:show="showDate" 
			mode="multiple" 
			monthNum="5" 
			:minDate="minDate" 
			:defaultDate="defaultDateMultiple">
		</u-calendar>
		
		<u-toast ref="uToast" duration="3000" />
		
		<uni-fab ref="fab"
			:pattern="pattern" 
			:content="content" 
			:horizontal="horizontal" 
			:vertical="vertical"
			:direction="direction" 
			@trigger="trigger" 
			@fabClick="fabClick" />
			
		<!-- 报工数据变更 -->
		<u-popup :show="updateConfirmShow" mode="top"  @close="updateConfirmClose" :closeOnClickOverlay="false">
			<view style="max-height: 590rpx; overflow-y: auto;">
				<uni-table ref="table" :loading="updateConfirmLoading"
					border
					stripe 
					type="" 
					emptyText="暂无更多数据">
				<uni-tr style="background-color: #dbfbdb;">
					<uni-th align="left" width="5" style="font-size: 12px;color: #62c188;">序号</uni-th>
					<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">物料编码</uni-th>
					<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">物料名称</uni-th>
					<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">规格</uni-th>
					<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">当天合格数</uni-th>
					<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">当天不良品数</uni-th>
					<uni-th align="left" width="50" style="font-size: 12px;color: #62c188;">站点名称</uni-th>
					<uni-th align="left" style="font-size: 12px;color: #62c188;">备注</uni-th>
				</uni-tr>
				
				<uni-tr>
					<uni-td align="left" style="font-size: 12px;text-align: left;">1</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ update.bomCode }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ update.bomName }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ update.bomStandard }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ update.goodNum }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ update.ungoodNum }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ update.stationName }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ update.remark }}</uni-td>
				</uni-tr>
				</uni-table>
			</view>
			<view style="padding-bottom: 14rpx;">
				<uni-section title="变更信息" type="line" padding>
					<uni-forms-item label="合格数" name="goodNum">
						<uni-easyinput type="text" v-model="update.goodNumReq" placeholder="请输当天合格数"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item label="不良品数" name="ungoodNum">
						<uni-easyinput type="text" v-model="update.ungoodNumReq" placeholder="请输入当天不良品数"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item label="生产备注" name="remark">
						<uni-easyinput type="text" v-model="update.remarkReq" placeholder="请输入生产备注"></uni-easyinput>
					</uni-forms-item>
				</uni-section>
				<u-row style="padding-top: 4rpx;">
					<u-col span="6">
						<u-button
							type="error"
							text="关闭"
							shape="circle" 
							customStyle="width: 260rpx;"
							@click="updateConfirmShow = !updateConfirmShow"
						></u-button>
					</u-col>
					<u-col span="6">
						<u-button
							type="primary"
							text="提交"
							shape="circle" 
							customStyle="width: 260rpx;"
							@click="updateConfirm"
						></u-button>
					</u-col>
				</u-row>
			</view>
		</u-popup>
		
		<u-modal :show="noStationModalShow" 
			title="告警提示" content='该班组下没有配置站点,请先配置站点再操作!' 
			@confirm="noStationModalConfirm"></u-modal>
			
		<u-loadmore :status="loadStatus" />
		<u-notify ref="uNotify"></u-notify>
	</view>
</template>

<script>
const d = new Date();
const year = d.getFullYear();
let month = d.getMonth() + 1;
month = month < 10 ? `0${month}` : month;
const date = d.getDate();

import * as submitApi from '@/api/work/worksubmit';
import * as BaseApi from '@/api/base/base';
import storage from '@/utils/storage';
import * as userApi from '@/api/user';

export default {
	components: {
	},
	data() {
		return {
			// 上一个页面传入的参数
			contractCode: "",
			workerorderCode: "",
			
			// 当前 tab 动态计算
			workorderNum:  0,
			hasCompletionNum: 0,
			
			// 所有动态数组
			workOrderDoubleList: [],
			
			// 该工单下的历史记录
			contractHistoryDetailList: [],
			
			// 悬浮按钮开始
			pattern: {
				color: '#7A7E83',
				backgroundColor: '#fff',
				selectedColor: '#007AFF',
				buttonColor: '#007AFF',
				iconColor: '#fff',
				icon: 'gear'
			},
			content: [{
					iconPath: '/static/picking/refresh.png',
					selectedIconPath: '/static/picked/refresh.png',
					text: '刷新当前',
					active: false
				},
				{
					iconPath: '/static/submit/goback.png',
					selectedIconPath: '/static/submit/goback.png',
					text: '返回上页',
					active: false
				},
				// {
				// 	iconPath: '/static/picking/submit.png',
				// 	selectedIconPath: '/static/picking/submit_selected.png',
				// 	text: '提交报工',
				// 	active: false
				// }
			],
			horizontal: 'right',
			vertical: 'bottom',
			direction: 'vertical',
			// 悬浮按钮结束
			
			workOrderList: [],
			completionWorkList: [],
			
			loadingShow: false,
			submitInfoShow: false,
			submitTotal: 0,
			submitUngoodTotal: 0,
			tabList: [],
			show: false,
			
			// 查询参数，比如：合同号
			bomInfoParam: "",
			
			submitDateList: [],
			checked: ["选中"],
			// u-checkbox-group的v-model绑定的值如果设置为某个checkbox的name，就会被默认选中
			checkboxValue1: [],
			selectedCard: null, // 选中的卡片ID
			trueValue: true,
			// 日期弹窗
			showDate: false,
			minDate: `${year}-${month}-${date-30}`,
			defaultDateMultiple: [],
			
			isVisible: true,
			clickCount: 0,
			
			queryDateBtnText: "择查询日期",
			detailLoading: false,
			multiCollaspe: "",
			
			currentTabIndex: 0,
			currentSelectedList: [],
			
			currentCollapseIndex: null,
			
			loadStatus: "loadmore",
			total: 0,
			pageNum: 1,
			totalPageCount: 0,
			
			station: null,
			
			submitConfirmLoading: false,
			submitConfirmShow: false,
			errMessage: null,
			
			// 没有站点时，必须弹出模式窗，不能继续操作
			noStationModalShow: false,
			
			// 修改数据逻辑
			update: {
				goodNumReq: null,
				ungoodNumReq: null,
				remarkReq: null,
			},
			updateConfirmShow: false,
			updateConfirmLoading: false,
			
			notifyData: {
				top: 4,
				message: 'notify顶部提示',
				type: 'warning',
				color: '#ffffff',
				bgColor: '',
				fontSize: 12,
				duration: 2500,
				safeAreaInsetTop: false
			},
			user: {},
			
			factoryId: 0,
			teamId: 0,
			teamInfo: null,
		}
	},
	onLoad (options) {
		// TODO 临时测试数据
		this.defaultDateMultiple.push(`${year}-${month}-${date-1}`);
		this.defaultDateMultiple.push(`${year}-${month}-${date}`);
		if (options.factoryId) {
			this.factoryId = options.factoryId;
		}
		if (options.teamId) {
			this.teamId = options.teamId;
		}
		if (options.contractCode) {
			this.contractCode = decodeURIComponent(options.contractCode);
		}
		if (options.workerorderCode) {
			this.workerorderCode = decodeURIComponent(options.workerorderCode);
		}
		
		this.loadTeamInfoById();
		this.queryHistoryByParam();
		this.loadUserFromCache();
	},
	created() {
		console.log("tabIndex, contractCode", this.currentTabIndex, this.contractCode);
		// this.initStationList();
	},
	methods: {
		loadTeamInfoById() {
			userApi.getTeamById({teamId: this.teamId}).then(response => {
				if (response && response.data) {
					this.teamInfo = response.data;
				}
			}).then(res => {
				console.log("test:", this.teamInfo);
			});
			
		},
		loadUserFromCache() {
			this.$store.dispatch('Info').then(res => {
				console.log("刷新获取最新用户:", res);
				this.user = res;
			});
		},
		onReachBottom() {
			if(this.pageNum > this.totalPageCount 
				|| this.contractHistoryDetailList.length == this.total) {
				console.log("已经加载到最多了");
				return;
			}
			this.pageNum ++;
			this.queryContractHistoryDetail();
		},
		updateConfirmClose() {
			// 关闭
			this.updateConfirmShow = false;
		},
		updateShow(item) {
			console.log(item);
			if (!item.creator) {
				this.notifyError("该提交记录不能修改～001");
				return;
			}
			// item.creator != this.user.nickName
			if (item.creator !== this.user.nickName) {
				this.notifyError(`该记录是 '${item.creator}' 提交的数据，你无权修改！002`);
				return;
			}
			this.updateConfirmShow = true;
			this.update = item;
			this.update.goodNumReq = item.goodNum;
			this.update.ungoodNumReq = item.ungoodNum;
			this.update.remarkReq = item.remark;
		},
		notifyError(message) {
			this.notifyData.type = 'error';
			this.notifyData.message = message;
			this.$refs.uNotify.show({
				...this.notifyData
			});
		},
		updateConfirm() {
			// 提交
			console.log("报工修改真正提交:", this.update);
			let param = {
				id: this.update.id,
				goodNum: this.update.goodNumReq,
				ungoodNum: this.update.ungoodNumReq,
				remark: this.update.remarkReq,
			};
			submitApi.updateSubmitInfo(param).then(res => {
				// 提交结果
				console.log("报工修改真正提交成功",  res);
				if (res.code == 200) {
					this.$refs.uToast.success(`报工修改提交成功!}`);
					this.queryHistoryByParam();
					this.updateConfirmShow = false;
				} else {
					this.$refs.uToast.error(`报工修改提交失败,${res.msg}`);
				}
			}).catch(err => {
				// 提交异常
				console.error("报工真正提交发生异常", err);
			});
		},
		noStationModalConfirm() {
			this.navigateTo("/pages/work/dayreport/worksubmitViewB");
		},
		submitConfirmClose() {
			this.submitConfirmShow = false;
		},
		queryHistoryByParam() {
			this.contractHistoryDetailList = [];
			this.pageNum = 1;
			this.queryContractHistoryDetail();
		},
		queryContractHistoryDetail() {
			this.loadingShow = true;
			// this.contractHistoryDetailList = [];
			// const submitDates = [submitDate];
			submitApi.querySubmitHistoryBySubmitDate({
				pageNum: this.pageNum,
				pageSize: 8,
				contractCode: this.contractCode, 
				factoryId: this.factoryId,
				teamId: this.teamId,
				bomInfoParam: this.bomInfoParam,
				// submitDateList: submitDates,
			}).then(res => {
				console.log("查询已报工的历史记录详情成功:", res);
				if(res && res.rows) {
					const result = res.rows || [];
					for (let item of result) {
						this.contractHistoryDetailList.push(item);
					}
					
					if (result.length === 0) {
						this.loadStatus = 'nomore';
						return;
					}
				}
				
				this.loadingShow = false;
				
				// 分页查询
				this.totalPageCount = res.totalPageCount || 0;
				this.total = res.total || 0;
				if (this.pageNum >= this.totalPageCount || this.contractHistoryDetailList.length === this.total) {
					this.loadStatus = 'nomore';
				} else {
					this.loadStatus = 'loading';
				}
			}).catch(err => {
				console.error("查询已报工的历史记录详情异常:", err);
				this.loadingShow = false;
			});
		},
		initStationList() {
			BaseApi.getStationList({teamId: this.teamId}).then(res => {
				  console.log("站点列表:", res);
				  if (!res || !res.data || res.data.length == 0) {
					  this.tabList = [];
					  this.noStationModalShow = true;
					  return;
				  }
				  this.tabList = res.data.map((item) => {
					return {
					  stationId: item.stationId,
					  stationName: item.stationName,
					  name: item.stationName,
					};
				  });
				  for (let i=0;i<this.tabList.length;i++) {
					  this.tabList[i].index = i;
					  this.workOrderDoubleList[i] = [];
				  }
				  this.queryWaitingCompletionList(this.tabList[0]);
			});
		},
		fabClick() {
		},
		todayRecvNumChange(productItem, e) {
			if (!productItem.todayCompletionReq) {
				return;
			}
			if (!(!isNaN(parseFloat(productItem.todayCompletionReq)) && isFinite(productItem.todayCompletionReq)) 
				|| productItem.todayCompletionReq <= 0) {
				this.$refs.uToast.error(`输入必须是大于 0 的数字`);
				return;
			}
			if (Number(productItem.todayCompletionReq) > productItem.remainingCompletionNum) {
				this.$refs.uToast.error(`${productItem.bomName}, 规格：${productItem.bomStandard} 最多还剩数量:${productItem.remainingCompletionNum} 未报工,请确认`);
				productItem.todayCompletionReq = productItem.remainingCompletionNum;
			}
		},
		ungoodNumReqChange(productItem, e) {
			if (!productItem.ungoodNumReq) {
				return;
			}
			
			if (!(!isNaN(parseFloat(productItem.ungoodNumReq)) && isFinite(productItem.ungoodNumReq)) 
				|| productItem.ungoodNumReq <= 0) {
				this.$refs.uToast.error(`输入必须是大于 0 的数字`);
				return;
			}
			if (Number(productItem.ungoodNumReq) >= productItem.workorderNum) {
				this.$refs.uToast.error(`输入不良品不可能比订单总数还多`);
				productItem.ungoodNumReq = null;
			}
		},
		clearRecvNum(row, e) {
			console.log("row->", row);
		},
		// 根据搜索条件查询
		searchWaitingCompletionList() {
			// let curSelectedTab = this.tabList[this.currentTabIndex];
			// this.queryWaitingCompletionList(curSelectedTab, true);
		},
		// TODO 连接后台服务
		queryWaitingCompletionList(item, isRefresh) {
			console.log("查询待报工的订单请求参数:", item);
			this.loadingShow = true;
			this.workOrderList = [];
			this.workorderNum = 0;
			this.hasCompletionNum = 0;
			submitApi.getWaitingCompletionList({
				pageNum: 1,
				pageSize: 200,
				stationId: item.stationId,
				factoryId: this.factoryId,
				teamIdList: [this.teamId],
				contractCode: this.contractCode,
				bomInfoParam: this.bomInfoParam,
			}).then(res => {
				console.log("查询待报工的订单数据成功:", res);
				if(res && res.rows) {
					this.workOrderList = res.rows || [];
					
					if (isRefresh) {
						// 直接赋值是无法触发 vue更新 的，需要this.$set方式才会触发
						// this.workOrderDoubleList[item.index] = res.rows || [];
						this.$set(this.workOrderDoubleList, item.index, res.rows || []);
					} else {
						const preWorkOrderList = this.workOrderDoubleList[item.index];
						if (!(preWorkOrderList && preWorkOrderList.length > 0)) {
							this.workOrderDoubleList[item.index] = res.rows || [];
						}
					}
					console.log("workOrderDoubleList", this.workOrderDoubleList);
				}
				for (let obj of this.workOrderList) {
					this.workorderNum += obj.workorderNum ? obj.workorderNum : 0;
					this.hasCompletionNum += obj.hasCompletionNum ? obj.hasCompletionNum : 0;
				}
				this.loadingShow = false;
			}).catch(err => {
				console.error("查询待报工的订单数据异常:", err);
				this.loadingShow = false;
			});
		},
		tabClick(e) {
			console.log("tabClick,currentTabIndex", e, this.currentTabIndex);
			const preIndex = this.currentTabIndex;
			this.currentTabIndex = e.index;
			if (preIndex != e.index) {
				this.queryWaitingCompletionList(e);
			}
			this.station = e;
		},
		floatOnclick(e) {
			// TODO 扫描按钮事件
			console.log("扫描按钮事件:", e);
			// alert("正在打开飞书摄像头扫码");
			let _this = this;
			window.h5sdk.ready(() => {
				tt.scanCode({
					scanType: ["barCode", "qrCode", "datamatrix", "pdf417"],
					barCodeInput: true,
					success(res) {
						console.log("扫描返回:", res);
						// alert("成功:"+JSON.stringify(res));
						_this.contractCode = res.result;
						_this.searchWaitingCompletionList();
					},
					fail(res) {
						console.log(`scanCode fail:`, res);
						alert("失败:"+JSON.stringify(res));
					},
				});
			});
		},
		calendarConfirm(selectedItem) {
			console.log("日历选中", selectedItem);
			this.submitDateList = selectedItem;
			this.showDate = false;
			this.defaultDateMultiple = selectedItem;
			this.queryDateBtnText = "清空查询日期";
			this.searchWaitingCompletionList();
		},
		calendarClose() {
			this.showDate = false;
		},
		open() {
			// console.log('open');
			document.body.style.overflow = 'hidden'; // 禁用背景页面滚动
		},
		close() {
			this.show = false
			// console.log('close');
			document.body.style.overflow = ''; // 恢复背景页面滚动
		},
		reset() {
		},
		trigger(e) {
			console.log("flowButton.e, multiCollaspe", e, this.multiCollaspe);
			const btn = this.content[e.index];
			btn.active = true;
			if (btn.text === '刷新当前') {
				this.queryHistoryByParam();
				btn.active = false;
			} else if (btn.text === '返回上页') {
				this.goBack();
			}
		},
		goBack() {
			const contractCode = encodeURIComponent(this.contractCode);
			const workerorderCode = encodeURIComponent(this.workerorderCode);
			const url = `/pages/work/dayreport/worksubmitDetailB?contractCode=${contractCode}&workerorderCode=${workerorderCode}`;
			this.navigateTo(url);
		},
		navigateTo(url) {
			uni.navigateTo({ url: url });
		},
		submitCompletion() {
			const submitWorkOrderList = this.workOrderDoubleList[this.currentTabIndex];
			if (!submitWorkOrderList || submitWorkOrderList.length == 0) {
				this.$refs.uToast.error(`报工数据为空，提交失败`);
				return;
			}
			// 开始计算进行提交数据是否存在异常
			this.completionWorkList = [];
			let total = 0;
			let unGoodTotal = 0;
			for(let item of submitWorkOrderList) {
				if (!item.todayCompletionReq && Number(item.todayCompletionReq) <= 0
					&& !item.ungoodNumReq && Number(item.ungoodNumReq) <= 0) {
					continue;
				}
				this.completionWorkList.push({
					contractCode: item.contractCode,
					bomName: item.bomName,
					bomStandard: item.bomStandard,
					workerorderCode: item.workerorderCode,
					bomCode: item.bomCode,
					todayCompletionReq: item.todayCompletionReq,
					ungoodNumReq: item.ungoodNumReq,
					remark: item.remark,
				});
				total += Number(item.todayCompletionReq);
				unGoodTotal += Number(item.ungoodNumReq);
			}
			if (this.completionWorkList.length == 0) {
				this.$refs.uToast.error(`今日报工数都是 0，请先输入`);
				return;
			}
			
			this.submitTotal = total;//  合格产品
			this.submitUngoodTotal = unGoodTotal;// 不良品
			this.submitConfirmShow = true;
		},
		submitReally() {
			let station = this.tabList[this.currentTabIndex];
			for(let item of this.completionWorkList) {
				item.stationId = station.stationId;
				item.stationName = station.stationName;
				if (this.errMessage) {
					if (item.remark) {
						item.remark = item.remark + "," + this.errMessage;
					} else {
						item.remark = this.errMessage;
					}
				}
			}
			console.log("报工真正提交:", this.completionWorkList);
			submitApi.newSubmitStationDay(this.completionWorkList).then(res => {
				// 提交结果
				console.log("报工真正提交成功",  res);
				if (res.code == 200) {
					this.$refs.uToast.success(`今日报工提交成功,完工数:${this.submitTotal}`);
					this.searchWaitingCompletionList();
					this.submitConfirmShow = false;
					this.errMessage = null;
				} else {
					this.$refs.uToast.error(`今日报工提交失败,${res.msg}`);
				}
			}).catch(err => {
				// 提交异常
				console.error("报工真正提交发生异常", err);
			});
		},
		goToPre() {
			console.log("返回主页");
			uni.reLaunch({ url: '/pages/work/index' });
		},
	}
}
</script>

<style lang="scss" scoped>
/deep/.u-cell__body {
  // padding: 6px 0 !important;
  display: flex;
  flex-direction: row;
  box-sizing: border-box;
  padding: 6px 8px !important;
  font-size: 15px;
  color: #303133;
  align-items: center;
}
/deep/.uni-forms-item is-direction-left {
	position: relative;
	display: flex;
	margin-bottom: 6px !important;
	flex-direction: row;
}
.card-container{
	width: 100%;
	min-height: 100%;
	box-sizing: border-box;
	padding: 10rpx;
	background-color: #ECECEC;
}
.card-wrap{
	padding: 12rpx;
	border-radius: 12rpx;
	margin-bottom: 12rpx;
	background-color: #FFF;
	border: 1px solid #ddd; /* 默认边框 */
	transition: background-color 0.3s, border-color 0.3s;
	.card-item{
		display: flex;
		align-items: center;
		font-size: 28rpx;
		padding-left: 6rpx;
		padding-bottom: 18rpx;
		color: #40485d;
	}
	.card-item-txt{
		flex:1;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
}
.card-btns{
	display: flex;
	justify-content: space-between;
	align-items: center;
}
.card-btns uni-button {
  flex: 1;
  margin: 0 5px; /* 调整按钮间距 */
}
.card-wrap-selected {
  border-color: #007bff; /* 选中状态的边框颜色 */
  background-color: #e7f0ff; /* 选中状态的背景颜色 */
}
.gridcell {
  border: 1px solid #ddd;
  text-align: center;
  line-height: 20px; /* Adjust line-height to match height */
  height: 30px; /* Set height to 20px */
  background-color: #f9f9f9;
  font-size: 14px; /* Adjust font-size to fit */
  overflow: hidden; /* Hide overflow if text is too large */
}
.card-item-single{
	border-radius: 6rpx;
	background-color: #FFF;
	border: 1px solid #ddd; /* 默认边框 */
	transition: background-color 0.3s, border-color 0.3s;
	align-items: left;
	font-size: 30rpx;
	color: #40485d;
}

.submit {
	// background-color: #007cff;
	text-align: center;
	margin: 5px 15px 5px 5px;
}

.demo-layout {
	height: 25px;
	border-radius: 4px;
}
.bg-purple {
	background: #CED7E1;
}
.bg-purple-light {
	background: #e5e9f2;
}


.flex-item {
	width: 33.3%;
	height: 200rpx;
	text-align: center;
	line-height: 200rpx;
}

.flex-item-V {
	width: 100%;
	height: 150rpx;
	text-align: center;
	line-height: 150rpx;
}

.text {
	margin: 15rpx 10rpx;
	padding: 0 20rpx;
	background-color: #ebebeb;
	height: 70rpx;
	line-height: 70rpx;
	text-align: center;
	color: #777;
	font-size: 26rpx;
}

.desc {
	/* text-indent: 40rpx; */
}
.flex-pc {
	display: flex;
	justify-content: center;
}


	
/* 示例样式开始 */
.content {
	font-size: 28rpx;
	background-color: #f3f3f3;
	padding: 10rpx;
	
	.red{
		background-color: #DD524D;
		color: #fff;
		text-align: center;
	}
	
	.green{
		background-color: #42b983;
		color: #fff;
		text-align: center;
	}
	
	.blue{
		background-color: #007AFF;
		color: #fff;
		text-align: center;
	}
	
	.yellow{
		background-color: #ffaa00;
		color: #fff;
		text-align: center;
	}
	

	.itme-box {
		padding: 10rpx;
		background-color: #fff;
		margin-bottom: 10rpx;

		.title {
			padding-bottom: 12rpx;
			margin-bottom: 12rpx;
			border-bottom: #e5e5e5 solid 1px;
			line-height: normal;
		}
		
		// .width-fill{
		// 	width: 100%;
		// }
		
		.preview-box{
			height: 400rpx;
			box-shadow: 0 0 4px rgba(0,0,0,0.3);
		}
	}
}
/* 示例样式结束 */

.td_header {
	color: rgb(98, 193, 136);
	background-color: #dbfbdb;
}

</style>
